
pub struct SVG {
    buf: String,
}

impl SVG {
    pub fn new() -> Self {
        Self { buf: "".to_string() }
    }
}

impl SVG {
    pub fn start(&mut self, width_view: u32, height_view: u32) {
        let line = format!("<svg viewBox=\"0 0 {width_view} {height_view}\" xmlns=\"http://www.w3.org/2000/svg\">\n");
        self.buf.push_str(&line);
    }
    pub fn start2(&mut self, left: i32, bottom: i32, width_view: u32, height_view: u32) {
        let line = format!("<svg viewBox=\"{left} {bottom} {width_view} {height_view}\" xmlns=\"http://www.w3.org/2000/svg\">\n");
        self.buf.push_str(&line);
    }
    pub fn end(&mut self) {
        self.buf.push_str("</svg>");
    }
}

impl SVG {
    pub fn background(&mut self, color: &str) {
        let line = format!("\t<rect width=\"100%\" height=\"100%\" fill=\"{color}\" />\n");
        self.buf.push_str(&line);
    }
}

impl SVG {
    pub fn draw_line(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, color: &str, width: f64) {
        let line = format!("\t<line x1=\"{x1}\" y1=\"{y1}\" x2=\"{x2}\" y2=\"{y2}\" style=\"stroke:{color};stroke-width:{width}\" />\n");
        self.buf.push_str(&line);
    }
}

impl SVG {
    pub fn fill_circle(&mut self, x: f64, y: f64, r: f64, color: &str) {
        let line = format!("\t<circle cx=\"{x}\" cy=\"{y}\" r=\"{r}\" fill=\"{color}\"/>\n");
        self.buf.push_str(&line);
    }
}

impl SVG {
    pub fn fill_polygon(&mut self, 多边形: Vec<(f64, f64)>, 填充颜色: String) {
        let mut points = "".to_string();
        for (x, y) in 多边形 {
            points.push_str(&format!("{x},{y} "));
        }
        let line = format!("\t<polygon points=\"{points}\" fill=\"{填充颜色}\"/>\n");
        self.buf.push_str(&line);
    }
}

impl SVG {
    pub fn text(&mut self, x: f64, y: f64, text: &str, 字体大小: f64, 颜色: &str) {
        let line = format!("\t<text x=\"{x}\" y=\"{y}\" font-size=\"{字体大小}\" fill=\"{颜色}\">{text}</text>\n");
        self.buf.push_str(&line);
    }
}
